<?php
class Mozg_Token
{
	protected static $_issetCookie = false;
	protected static $_cookieName = '_token';

	static function Get($name = '')
	{
		$token = Mozg::Request('http')->getCookie(self::$_cookieName);
		if (!strlen($token))
		{
			$token = self::Generate();
			if (!self::$_issetCookie)
			{
				Mozg::Request('http')->SetCookie(self::$_cookieName, $token, 0, _ROOT_URL);
				self::$_issetCookie = true;
			}
		}

		if (!strlen($name)) $name = 'default';
		return md5($token.$name.$_SERVER["HTTP_USER_AGENT"]);
	}

	static protected function Generate()
	{
		$token = uniqid('', true);
		return $token;
	}

	static public function Validate($name, $token)
	{
		return (self::Get($name) == $token);
	}
}